# Copyright 1999-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: mail-mta/meta1/meta1-1.0_alpha13.ebuild,v 1.1 2013/04/17 09:30:00 itspec.ru Exp $

EAPI="3"

inherit eutils db-use autotools flag-o-matic

DESCRIPTION="MeTA1 is a Message Transfer Agent (MTA)"
HOMEPAGE="http://www.MeTA1.org/"

# Original name (including version)
MY_P="meta1-1.0.Alpha13.0"

SRC_URI="http://www.meta1.org/download/.alpha/calist/${MY_P}.tar.gz"

LICENSE="Sendmail"
RESTRICT="mirror"
SLOT="0"
KEYWORDS="~x86"
IUSE="+system-berkdb +msp +tls +sasl +pmilter +tinycdb dkim test timing"

DEPEND=">=sys-libs/db-4.1
        tls? ( >=dev-libs/openssl-0.9.6 )
        sasl? ( >=dev-libs/cyrus-sasl-2.1.18 )
        tinycdb? ( >=dev-db/tinycdb-0.75 )
        sys-devel/m4
        "

RDEPEND="${DEPEND}
        net-mail/mailbase
        !net-mail/vacation
        msp? ( !net-mail/mailwrapper )
        !mail-mta/courier
        !mail-mta/esmtp
        !mail-mta/exim
        !mail-mta/mini-qmail
        !mail-mta/msmtp
        !mail-mta/nbsmtp
        !mail-mta/netqmail
        !mail-mta/nullmailer
        !mail-mta/postfix
        !mail-mta/qmail-ldap
        !mail-mta/sendmail
        !mail-mta/ssmtp
        "

#PROVIDE="virtual/mta"

S=${WORKDIR}/${MY_P}

src_unpack() {
    unpack $A
}

src_prepare() {
    if use system-berkdb; then
        #epatch "${FILESDIR}"/meta1-add-db47-support-1.patch
        epatch "${FILESDIR}"/meta1-add-db47-support-2.patch
        epatch "${FILESDIR}"/meta1-add-db47-support-3.patch
        epatch "${FILESDIR}"/meta1-add-db47-support-4.patch
        #epatch "${FILESDIR}"/meta1-add-db47-support-5.patch
    fi

    # disable run sm.setup.sh at installation
    # due to runas failed in sandbox
    # sm.setup.sh must be runing by "ebuild ${PF}.ebuild config"
    epatch "${FILESDIR}"/meta1-disable-sm.setup.sh.patch
}

src_configure() {
    local bdbconf=""

    if use timing; then
        append-cflags -DSC_STATS
    fi

    if use system-berkdb; then
        bdbconf+="
                --disable-included-bdb \
                --with-bdb-libdir=/usr/$(get_libdir) \
                --with-bdb-incdir=$(db_includedir) \
                "
    fi

    econf \
        $(use_enable msp) \
        $(use_enable tls TLS) \
        $(use_enable sasl SASL) \
        $(use_enable pmilter) \
        $(use_enable tinycdb) \
        $(use_enable dkim DKIM) \
        ${bdbconf} \
        || die "econf failed"
}

src_compile() {
    emake -j1 || die "compile problem"
}

src_test() {
    emake -j1 check
}

src_install() {
    emake -j1 DESTDIR="${D}" install

    # configs
    insinto /etc/${PN}

    newins ${FILESDIR}/meta1-1.0_alpha42.conf meta1.conf
    fowners meta1:meta1 /etc/${PN}/meta1.conf

    #doins ${FILESDIR}/mt
    #fowners meta1m:meta1m /etc/${PN}/mt

    # configurations script and helpers binary
    exeinto /usr/libexec/meta1/checks
    doexe ${S}/checks/t-getgroup || die "Installing t-getgroup failed"

    exeinto /usr/libexec/meta1/libconf
    doexe ${S}/libconf/tree || die "Installing tree failed"

    exeinto /usr/libexec/meta1/libmta
    doexe ${S}/libmta/t-hostname || die "Installing t-hostname failed"
    doexe ${S}/libmta/statit || die "Installing statit failed"

    exeinto /usr/libexec/meta1/libcheck
    doexe ${S}/libcheck/noroot || die "Installing noroot failed"

    exeinto /usr/libexec/meta1/misc
    #doexe ${S}/misc/runas || die "Installing runas failed"
    dosym /usr/bin/runas /usr/libexec/meta1/misc/runas || die "Creating symlink to runas failed"
    doexe ${S}/misc/sm.setup.sh || die "Installing sm.setup.sh failed"
    doexe ${S}/misc/sm.check.sh || die "Installing sm.check.sh failed"

    # initscript
    newinitd "${FILESDIR}"/mcp.init mcp
    dodoc ${S}/doc/* || die "Installing docs failed"
}

pkg_setup() {
    # перенесён из pkg_preinst(), см. http://bugs.gentoo.org/217042

    einfo "checking for necessary groups and users...  create if missing.\n"

    enewgroup meta1s || die "problem adding group meta1s"
    enewgroup meta1q || die "problem adding group meta1q"
    enewgroup meta1c || die "problem adding group meta1c"
    enewgroup meta1m || die "problem adding group meta1m"
    enewgroup meta1  || die "problem adding group meta1"

    #enewuser: extra arguments no longer supported;
    #enewuser meta1s -1 -1 /dev/null meta1s,meta1c,meta1m -c 'meta1 SMTPS' || die "problem adding user meta1s"
    #enewuser meta1q -1 -1 /dev/null meta1q,meta1m        -c 'meta1 QMGR'  || die "problem adding user meta1q"
    #enewuser meta1c -1 -1 /dev/null meta1c               -c 'meta1 SMTPC' || die "problem adding user meta1c"
    #enewuser meta1m -1 -1 /dev/null meta1m               -c 'meta1 misc'  || die "problem adding user meta1m"
    #enewuser meta1  -1 -1 /dev/null meta1                -c 'meta1 other' || die "problem adding user meta1"

    enewuser meta1s -1 -1 /dev/null meta1s,meta1c,meta1m  || die "problem adding user meta1s"
    enewuser meta1q -1 -1 /dev/null meta1q,meta1m         || die "problem adding user meta1q"
    enewuser meta1c -1 -1 /dev/null meta1c                || die "problem adding user meta1c"
    enewuser meta1m -1 -1 /dev/null meta1m                || die "problem adding user meta1m"
    enewuser meta1  -1 -1 /dev/null meta1                 || die "problem adding user meta1"
}

pkg_postinst() {
    elog
    elog "For initial configuration please execute"
    elog "\`${DESTTREE}/libexec/meta1/misc/sm.setup.sh\`"
    elog
    elog "To start the MeTA1 by default you should add 'mcp' to the default runlevel:"
    elog "\`rc-update add mcp default\`"
    elog
}

pkg_config() {
    if test -f ${DESTTREE}/libexec/meta1/misc/sm.setup.sh
    then
        einfo "For postinstall configuration please execute"
        einfo  "\`${DESTTREE}/libexec/meta1/misc/sm.setup.sh\`"
    else
        einfo "\`${DESTTREE}/libexec/meta1/misc/sm.setup.sh\` not exist. Please reemerge meta1"
    fi
}
